package com.webserver.controller;

import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import com.webserver.vo.Article;
import com.webserver.vo.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 处理与文章相关的业务
 */
public class ArticleController {

    public static final String ARTICLE_DIR = "./articles/";

    static {
        File dir = new File(ARTICLE_DIR);
        if (!dir.exists()) {//如果该目录不存在则自动创建
            dir.mkdirs();
        }
    }

    public void writeArticle(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理发表文章....");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");

        if (title == null || author == null || content == null) {
            File file = new File("./webapps/myweb/article_fail.html");
            response.setEntity(file);
            return;
        }

        try (
                FileOutputStream fos = new FileOutputStream(ARTICLE_DIR + title + ".obj");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            Article article = new Article(title, author, content);
            oos.writeObject(article);
            File file = new File("./webapps/myweb/article_success.html");
            response.setEntity(file);
        } catch (Exception e) {
            e.printStackTrace();
        }


        System.out.println("发表文章处理完毕!");
    }

    public void showAllArticle(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始查看文章");
        List<Article> articleList = new ArrayList<>();
        File dir = new File(ARTICLE_DIR);
        File[] subs = dir.listFiles(f -> f.getName().endsWith(".obj"));
        for (File articleFIle : subs) {
            try (FileInputStream fis = new FileInputStream(articleFIle);
                 ObjectInputStream ois = new ObjectInputStream(fis)) {
                Article article = (Article) ois.readObject();
                articleList.add(article);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println(articleList);

        PrintWriter pw = response.getWrite();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>用户列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<table>");
        pw.println("<h1>文章列表</h1>");
        pw.println("<table border=\"3\">");
        pw.println("<tr>");
        pw.println("<td>标题</td>");
        pw.println("<td>作者</td>");

        pw.println("</tr>");
        for (Article article : articleList) {
            pw.println(" <tr>");
            pw.println("<td><a href=\"" + article.getTitle() + "\">" + article.getTitle() + "</a></td>");
            pw.println("<td>" + article.getAuthor() + "</td>");

            pw.println(" </tr>");
        }
        pw.println("<tr>");
        pw.println(" <td colspan=\"3\" align=\"center\">");
        pw.println("<a href=\"index.html\">返回首页</a>");
        pw.println("</td>");
        pw.println("<tr>");
        pw.println("</table>");

        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");
        response.setContentType("text.html");
        System.out.println("页面处理完毕");
    }

    public static String watch() {
        String title = null;
        List<Article> articleList = new ArrayList<>();
        File dir = new File(ARTICLE_DIR);
        File[] subs = dir.listFiles(f -> f.getName().endsWith(".obj"));
        for (File articleFIle : subs) {
            try (FileInputStream fis = new FileInputStream(articleFIle);
                 ObjectInputStream ois = new ObjectInputStream(fis)) {
                Article article = (Article) ois.readObject();
                articleList.add(article);
                title = article.getTitle();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return title;
    }
    public void watchArticle(HttpServletRequest request, HttpServletResponse response){
        List<Article> articleList = new ArrayList<>();
        Article article = null;
        File dir = new File(ARTICLE_DIR);
        File[] subs = dir.listFiles(f -> f.getName().endsWith(".obj"));
        for (File articleFIle : subs) {
            try (FileInputStream fis = new FileInputStream(articleFIle);
                 ObjectInputStream ois = new ObjectInputStream(fis)) {
                article = (Article) ois.readObject();
                articleList.add(article);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        File articles = new File(ARTICLE_DIR+article.getTitle()+".obj");
        try(FileInputStream fis = new FileInputStream(articles);
            ObjectInputStream ois = new ObjectInputStream(fis)) {
            Article article1 = (Article) ois.readObject();
            articleList.add(article1);
            PrintWriter pw = response.getWrite();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>用户列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<table>");
            pw.println("<h1>文章</h1>");
            pw.println("<table border=\"1\">");
            pw.println("<tr>");
            pw.println("<td>标题</td>");
            pw.println("<td>作者</td>");
            pw.println("<td>内容</td>");
            pw.println("</tr>");
            for (Article article2 : articleList) {
                pw.println(" <tr>");
                pw.println("<td>" + article2.getTitle() + "</td>");
                pw.println("<td>" + article2.getAuthor() + "</td>");
                pw.println("<td>" + article2.getContent() + "</td>");
                pw.println(" </tr>");
            }
            pw.println("<tr>");
            pw.println(" <td colspan=\"3\" align=\"center\">");
            pw.println("<a href=\"index.html\">返回首页</a>");
            pw.println("</td>");
            pw.println("</table>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");

            //3 设置响应头Content-Type,告知浏览器这些动态数据是一个页面
            response.setContentType("text/html");

            System.out.println("动态页面处理完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
